import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'package:device_info_plus/device_info_plus.dart';

class DeviceInfoPage extends StatelessWidget {
  const DeviceInfoPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: const Text('设备信息'),
      ),
      body: Center(
        child: Column(children: [
          FutureBuilder<BaseDeviceInfo>(
            future: getDeviceInfo(),
            builder: (context, snapshot) {
              if (snapshot.connectionState == ConnectionState.done) {
                final deviceInfo = snapshot.data!;
                var str = "";
                for (var key in deviceInfo.data.keys) {
                  var element = deviceInfo.data[key];
                  str += '$key : $element \n';
                }
                return Text(str);
              } else {
                return CircularProgressIndicator();
              }
            },
          ),
        ]),
      ),
    );
  }

  Future<BaseDeviceInfo> getDeviceInfo() async {
    final DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
    if (defaultTargetPlatform == TargetPlatform.android) {
      return await deviceInfoPlugin.androidInfo;
    } else if (defaultTargetPlatform == TargetPlatform.iOS) {
      return await deviceInfoPlugin.iosInfo;
    } else {
      return BaseDeviceInfo({});
    }
  }
}
